// pages/make_appointment/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        card_id:0,
        hosptial_id:0,
        choice_date:null,
        info_id:0,
        info:null,
        appoint_mobile:'',
        appoint_code:'',
        seconds:0,
        is_send:true,
        user_name:'',
        timer:null,
        is_submit:false,
        tags_id:0
    },
    inputcode(e){
      this.setData({
          appoint_code:e.detail.value
      })
    },
    inputmobile(e){
        this.setData({
            appoint_mobile:e.detail.value
        })
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
   console.log(options)
   this.setData({
       card_id:options.card_id,
       hosptial_id:options.hosptial_id,
       tags_id:options.tags_id
   })
    },
    getInfo(){
        var that=this;
        
        request.requestLoading(
          "/api/mine/mineInfoDetail",
          {id:that.data.info_id},
          'POST',
        ).then((res) => {
          if (res.code == 1) {
             that.setData({
                 info:res.data
             })
             console.log(res.data)
             if(res.data){
                 that.setData({
                    user_name:res.data.medical_username
                 })
             }
            } else {
              functions.toast(res.msg)
            }
  
         
           
    
        }).catch((err) => {
          console.error(err);
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    sendSms()
    {
       
       
        
         
     let reg =/^1\d{10}$/;
      var json={};
   
      if(!this.data.appoint_mobile){
        request.toast('请输入联系人电话');
        return;
    }
      if(!reg.test(this.data.appoint_mobile)){
          request.toast('请输入正确的联系人电话');
          return;
      }

      json.mobile=this.data.appoint_mobile;
      json.event='notice';
      var that=this;
      request.requestLoading(
        "/api/sms/send",
        json,
        'POST',
      ).then((res) => {
        if (res.code == 1) {
            that.setData({
                seconds:60,
                is_send:false
            })
            
            
            that.data.timer = setInterval(() => {
                console.log(11)
                if (that.data.seconds > 0 ) {
                 
                  that.setData({
                    seconds: that.data.seconds-1,
                   
                })
                } else {
                 
                  clearInterval(that.data.timer);
                  that.setData({
                      seconds:60,
                      is_send:true,
                      timer:null
                  })
                  
                }
              }, 1000);
          } else {
            functions.toast(res.msg)
          }

       
         
  
      }).catch((err) => {
        console.error(err);
      });

    },
    submit()
    {
        let reg =/^1\d{10}$/;
        var that =this;
      var json={};
      console.log(that.data.choice_date)
      console.log(that.data.tags_id)
      if(!that.data.user_name){
        request.toast('请选择就诊人');
        return;
      }
      if(!that.data.choice_date){
        request.toast('请选择就诊时间');
        return;
      }
      /**
       *      var choice_date={ 'date':that.data.select_time[that.data.selectIndex[0]].date,
          'week_name':that.data.select_time[that.data.selectIndex[0]].week_name,
           'date_min':that.data.select_time[that.data.selectIndex[0]].date_min,
           'text':that.data.select_time[that.data.selectIndex[0]].date_min+'('+that.data.select_time[that.data.selectIndex[0]].week_name+')'+that.data.times_slot,
           'time_solt':that.data.times_slot};
       */
      console.log(that.data.choice_date);
      json.card_id=that.data.card_id;
      json.hosptial_id=that.data.hosptial_id;
      json.appoint_times=that.data.choice_date.time_solt;
      json.appoint_week_name=that.data.choice_date.week_name;
      json.appoint_date=that.data.choice_date.date;
      json.appoint_text=json.appoint_date+'('+json.appoint_week_name+')'+json.appoint_times;
      json.info_id=that.data.info_id;
      json.tags_id=that.data.tags_id;
    
      if(!this.data.appoint_mobile){
        request.toast('请输入联系人电话');
        return;
    }
      if(!reg.test(this.data.appoint_mobile)){
          request.toast('请输入正确的联系人电话');
          return;
      }
      if(that.data.is_submit){
        wx.showToast({
          title: '请勿重复预约',
          icon:'none'
        })
        return false
    }
    that.setData({
        is_submit:true
    })
        json.appoint_mobile=this.data.appoint_mobile;
        json.appoint_code=this.data.appoint_code;
        json.event='notice';
        var that=this;
        request.requestLoading(
          "/api/Appoint/addAppointment",
          json,
          'POST',
        ).then((res) => {
          if (res.code == 1) {
              wx.showToast({
                title: '预约成功',
                icon:'none',
                duration:1500
              })  
              setTimeout(function(){
                wx.navigateBack({
                    delta:1
                })
              },1500)
             
            } else {
              functions.toast(res.msg)
              that.setData({
                is_submit:false
            })
            }
  
         
           
    
        }).catch((err) => {
          console.error(err);
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

   
})